package com.wzdigit.wms.wms.service.basic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.wms.basic.domain.inventory.LotAttrDtl;
import com.wzdigit.wms.common.utils.DozerUtils;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.basic.client.request.basic.CreateLotAttrDtlRequest;
import com.wzdigit.wms.basic.client.request.basic.CreateUpdateLotAttrDtlListRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.LotAttrDtlResponse;
import com.wzdigit.wms.basic.mapper.core.basic.LotAttrDtlMapper;
import net.dreamlu.mica.core.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 批次属性规则明细-服务类
 * @author yangdongjie
 * @LotAttrDtlService.java
 * @2021年1月19日下午2:22:41
 */
@Service
public class LotAttrDtlService {

	@Autowired
    private LotAttrDtlMapper lotAttrDtlMapper;
	
	@Autowired
    private LotAttrService lotAttrService;
	
	/**
	 * 根据批次属性ID查询明细列表
	 * @param lotAttrId
	 * @return
	 */
	public List<LotAttrDtlResponse> getByLotAttrId(Integer lotAttrId) {
		QueryWrapper<LotAttrDtl> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("LOT_ATTR_ID", lotAttrId);
		queryWrapper.orderByAsc("ATTR_SEQ");
		List<LotAttrDtl> list = lotAttrDtlMapper.selectList(queryWrapper);
		return DozerUtils.mapList(list, LotAttrDtlResponse.class);
    }
	
    /**
     * 批量创建修改删除批次属性明细
     * @param dto
     * @param servletRequest
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public <T> Result<T> createUpdateListLotAttrDtl(CreateUpdateLotAttrDtlListRequest dto,
    		HttpServletRequest servletRequest) {
    	lotAttrService.ifCanUpdate(dto.getLotAttrId());
    	
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
		
    	// 记录不删除的属性明细ID
    	List<Integer> notDelIds = new ArrayList<Integer>();
    	
    	LotAttrDtl lotAttrDtl = null;
		for (CreateLotAttrDtlRequest one : dto.getDtls()) {
			if (one.getLotAttrDtlId() == null) {
				lotAttrDtl = new LotAttrDtl();
		    	BeanUtil.copyNonNull(one, lotAttrDtl);
		    	lotAttrDtl.setLotAttrId(dto.getLotAttrId());
		    	lotAttrDtl.setCreateUser(loginName);
		    	lotAttrDtl.setCreateTime(new Date());
		    	lotAttrDtlMapper.insert(lotAttrDtl);
			} else {
				lotAttrDtl = lotAttrDtlMapper.selectById(one.getLotAttrDtlId());
				BeanUtil.copy(one, lotAttrDtl);
		    	lotAttrDtl.setModifyUser(loginName);
		    	lotAttrDtl.setModifyTime(new Date());
		    	lotAttrDtlMapper.updateById(lotAttrDtl);
			}
			notDelIds.add(lotAttrDtl.getLotAttrDtlId());
		}
		
		// 删除属性明细
		QueryWrapper<LotAttrDtl> deleteWrapper = new QueryWrapper<>();
		deleteWrapper.eq("LOT_ATTR_ID", dto.getLotAttrId());
		if (notDelIds.size() > 0) {
			deleteWrapper.notIn("LOT_ATTR_DTL_ID", notDelIds);
			lotAttrDtlMapper.delete(deleteWrapper);
		} else {
			lotAttrDtlMapper.delete(deleteWrapper);
		}
		return new Result<>();
    }
}
